package com.tg.message.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.appbase.custom.base.MessageFilterBean;

import java.util.List;
import com.tg.message.R;

public class MessageFilterAdapter extends RecyclerView.Adapter<MessageFilterAdapter.MessageDeviceFilterViewHolder>{
    public static final int  FILTER_TYPE_DEVICE = 1 ;
    public static final int  FILTER_TYPE_MESSAGE_TYPE = 2 ;
    protected List<MessageFilterBean> list ;
    protected int mFilterType ;
    private int mSelectIndex;
    protected Context context;
    protected List<String> mSelectList;
    protected OnItemClickListener clickListener;

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setSelectList(List<String> mSelectList) {
        this.mSelectList = mSelectList;
    }

    public MessageFilterAdapter(List<MessageFilterBean> list){
        this.list = list;
    }

    public void setFilterType(int type){
        mFilterType = type;
    }
    public void setSelectIndex(int index) {
        this.mSelectIndex = index;
    }

    public int getFilterType() {
        return mFilterType;
    }

    public MessageFilterBean getItemById(int position) {
        return list==null||list.size()<position?null:list.get(position);
    }

    @NonNull
    @Override
    public MessageFilterAdapter.MessageDeviceFilterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_message_device_filter_item,parent,false);
        return new MessageDeviceFilterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageFilterAdapter.MessageDeviceFilterViewHolder holder, @SuppressLint("RecyclerView") int position) {
        MessageFilterBean messageFilterBean = list.get(position);
        holder.tvDeviceName.setText(messageFilterBean.name);
        holder.ivMultiSelected.setVisibility(View.GONE);
        if (position == mSelectIndex){
            holder.tvDeviceName.setBackgroundResource(R.drawable.main_10_rounded_bg);
            holder.tvDeviceName.setTextColor(context.getResources().getColor(R.color.global_customize_main_color));
        }else {
            holder.tvDeviceName.setBackgroundResource(R.drawable.dark_rounded_bg);
            holder.tvDeviceName.setTextColor(context.getResources().getColor(R.color.message_filter_item_text_normal_color));
        }
        holder.tvDeviceName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener!=null){
                    clickListener.onItemClick(position,mFilterType);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    public class MessageDeviceFilterViewHolder extends RecyclerView.ViewHolder {
         TextView  tvDeviceName;
         ImageView ivMultiSelected;
        public MessageDeviceFilterViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDeviceName = itemView.findViewById(R.id.tv_message_device_name);
            ivMultiSelected = itemView.findViewById(R.id.iv_message_multi_selected);
        }
    }
    public interface OnItemClickListener {
        void onItemClick(int position, int type);
    }
}
